#include "Matrix_buttons.h"

volatile int Matrix_buttons_now = 0;
volatile int Matrix_buttons_past = 16;
volatile int Matrix_buttons_x = 0;
volatile int Matrix_buttons_item = 0; 
volatile boolean Matrix_buttons_f1 = 1;
volatile boolean Matrix_buttons_f2 = 0;
volatile boolean Matrix_buttons_f3 = 0;
volatile int Matrix_buttons_qz = 0; 
volatile int Matrix_buttons_m = 0; 

Button_channel_t B_t;

void Matrix_buttons::init_button( KEY password_1 , KEY password_2 , KEY password_3){
  Wire.begin();
  
  Wire.beginTransmission(0x27);
  Wire.write(1);
  Wire.write(password_1);
  Wire.write(password_2);
  Wire.write(password_3);
  Wire.endTransmission();
}

uint16_t Matrix_buttons::Read_button(){
  Wire.beginTransmission(0x27);
  Wire.write(3);
  Wire.endTransmission();
  Wire.requestFrom(0x27, 1);
  while (Wire.available()) {
    Matrix_buttons_now = Wire.read();
      if(Matrix_buttons_now != Matrix_buttons_past){
        Matrix_buttons_past = Matrix_buttons_now;
        Serial.println(Matrix_buttons_now,DEC);
      }
    return Matrix_buttons_now;
  }   
}

void Matrix_buttons::Password(){
  Wire.beginTransmission(0x27);
  Wire.write(2);
  Wire.endTransmission();
  Wire.requestFrom(0x27, 2);
  while (Wire.available()) {
      Matrix_buttons_item = Wire.read();
      Matrix_buttons_x = Matrix_buttons_x + 1;
      if (Matrix_buttons_x % 2== 1) {
        Matrix_buttons_qz = Matrix_buttons_item;
        Matrix_buttons_m = 0;
  
      }
      if (Matrix_buttons_x == 2) {
        if (Matrix_buttons_item == 1 && Matrix_buttons_f1 == 1) {
          Matrix_buttons_f1 = 0;
          Matrix_buttons_f2 = 1;
          Matrix_buttons_f3 = 0;
          Serial.print(Matrix_buttons_qz,DEC);
          B_t.Read_Button[0] = Matrix_buttons_qz;
          Matrix_buttons_m = 0;
        }
  
      }
      if (Matrix_buttons_x == 4) {
        if (Matrix_buttons_item == 2 && Matrix_buttons_f2 == 1) {
          Matrix_buttons_f2 = 0;
          Matrix_buttons_f1 = 0;
          Matrix_buttons_f3 = 1;
          Serial.print(Matrix_buttons_qz,DEC);
          B_t.Read_Button[1] = Matrix_buttons_qz;
          Matrix_buttons_m = 0;
        }
  
      }
      if (Matrix_buttons_x == 6) {
        if (Matrix_buttons_item == 3 && Matrix_buttons_f3 == 1) {
          Matrix_buttons_f1 = 1;
          Matrix_buttons_f2 = 0;
          Matrix_buttons_f3 = 0;
          if (Matrix_buttons_qz == 18) {
            Serial.println("Wrong");
            B_t.Read_Button[2] = "Wrong";
            B_t.Read_Button[3] = 2;
            Matrix_buttons_m = 2;
          }
          if (Matrix_buttons_qz == 17) {
            Serial.println("OK");
            B_t.Read_Button[2] = "OK";
            B_t.Read_Button[3] = 1;
            Matrix_buttons_m = 1;
          }
        }
        Matrix_buttons_x = 0;
      }
    }
    // delay(10);
    // if(Matrix_buttons_x == 0 && Matrix_buttons_m == 1){
    //   // Matrix_buttons_m = 0;
    //   // for(int i = 0 ; i <= 3 ; i++){B_t.Read_Button[i] = " ";}
    //   return 1 ;
    // }else if(Matrix_buttons_x == 0 && Matrix_buttons_m == 2){
    //   // Matrix_buttons_m = 0;
    //   // for(int i = 0 ; i <= 3 ; i++){B_t.Read_Button[i] = " ";}
    //   return 2 ;
    // }else return Matrix_buttons_m;

}
void Matrix_buttons::Password_clear(){
    for(int i = 0 ; i <= 3 ; i++){
      B_t.Read_Button[i] = " ";
    }
}
